var express = require('express');
var router = express.Router();
var productDAL = require('../DAL/productDAL')

var jwt = require('jsonwebtoken')
const passport = require('passport');
const { token } = require('morgan');
// var app = require('../app')

//搜索商品
router.get('/getProductByKeywords', function (req, res, next) {
    var keyword = req.query
    productDAL.getProductByKeywords(keyword, function (err, results) {
        if (err) {
            res.json({
                code: 500,
                msg: '查找失败',
            })
        } else {
            res.json({
                code: 200,
                msg: '查找成功',
                results: results
            })
        }
    })
})
//多关键词搜索商品
router.get('/getProductByMoreKeywords', function (req, res, next) {
    var keys = req.query
    productDAL.getProductByMoreKeywords(keys, function (err,results) {
        if (err) {
            res.json({
                code: 500,
                msg: '查找失败',
            })
        } else {
            res.json({
                code: 200,
                msg: '查找成功',
                results:results
            })
        }
    })
})
//获取所有商品
router.get('/getAllProducts', function (req, res, next) {
    productDAL.getAllProducts(function (err, results) {
        if (err) {
            res.json({
                code: 500,
                msg: '获取所有商品失败'
            })
        } else {
            res.json({
                code: 200,
                msg: '获取所有商品成功',
                data: results
            })
        }
    })
})
//添加商品
router.post('/addProducts', function (req, res, next) {
    var products = req.body
    productDAL.addProducts(products, function (err, results) {
        if (err) {
            res.json({
                code: 500,
                msg: '增加失败'
            })
        } else {
            res.json({
                code: 200,
                msg: '增加成功',
                data: results
            })
        }
    })
})
//根据商品编号获取商品
router.get('/getProductById', function (req, res, next) {
    
    var products = req.query
    productDAL.getProductById(products, function (err, results) {
        if (err) {
            res.json({
                code: 500,
                msg: '获取商品失败'
            })
        } else {
            res.json({
                code: 200,
                msg: '获取商品成功',
                data: results
            })
        }
    })
})
//根据商品类型获取商品
router.get('/getProductByType', function (req, res, next) {
    var products = req.query
    productDAL.getProductByType(products, function (err, results) {
        if (err) {
            res.json({
                code: 500,
                msg: '获取商品失败'
            })
        } else {
            res.json({
                code: 200,
                msg: '获取商品成功',
                data: results
            })
        }
    })
})
//删除商品
router.post('/delProducts', function (req, res, next) {
    var products = req.body
    productDAL.delProducts(products, function (err, results) {
        if (err) {

            res.json({
                code: 500,
                msg: '删除商品失败'
            })
        } else {
            res.json({
                code: 200,
                msg: '删除商品成功'
            })
        }
    })
})
//修改商品信息
router.post('/changeProducts', function (req, res, next) {
    var products = req.body
    productDAL.changeProducts(products, function (err, results) {
        if (err) {
            res.json({
                code: 500,
                msg: '修改商品失败'
            })
        } else {
            res.json({
                code: 200,
                msg: '修改商品成功',
                results: results
            })
        }
    })
})
//根据发行时间获取商品
router.get('/getProductByDate',function(req,res,next){
    productDAL.getProductByDate(function(err,results){
        if(err){
            res.json({
                code: 500,
                msg: '获取商品失败'
            })
        } else {
            res.json({
                code: 200,
                msg: '获取商品成功',
                data: results
            })
        }
    })
})
//根据折扣获取商品
router.get('/getProductByDiscount',function(req,res,next){
    productDAL.getProductByDiscount(function(err,results){
        if(err){
            res.json({
                code: 500,
                msg: '获取商品失败'
            })
        } else {
            res.json({
                code: 200,
                msg: '获取商品成功',
                data: results
            })
        }
    })
})
//根据销量获取商品
router.get('/getProductBySales',function(req,res,next){
    productDAL.getProductBySales(function(err,results){
        if(err){
            res.json({
                code: 500,
                msg: '获取商品失败'
            })
        } else {
            res.json({
                code: 200,
                msg: '获取商品成功',
                data: results
            })
        }
    })
})
//根据收藏数量获取商品
router.get('/getProductByCollection',function(req,res,next){
    productDAL.getProductByCollection(function(err,results){
        if(err){
            res.json({
                code: 500,
                msg: '获取商品失败'
            })
        } else {
            res.json({
                code: 200,
                msg: '获取商品成功',
                data: results
            })
        }
    })
})
//根据浏览数量获取商品
router.get('/getProductByBrowse',function(req,res,next){
    productDAL.getProductByBrowse(function(err,results){
        if(err){
            res.json({
                code: 500,
                msg: '获取商品失败'
            })
        } else {
            res.json({
                code: 200,
                msg: '获取商品成功',
                data: results
            })
        }
    })
})
//添加购物车
router.post('/addShopCart', function (req, res, next) {
    var products = req.body
    productDAL.addShopCart(products, function (err, results) {
        if (err) {
            res.json({
                code: 500,
                msg: '添加购物车失败'
            })
        } else {
            res.json({
                code: 200,
                msg: '添加购物车成功',
                data: results
            })
        }
    })
})
//删除购物车
router.post('/delShopCart', function (req, res, next) {
    var products = req.body
    productDAL.delShopCart(products, function (err, results) {
        if (err) {
            res.json({
                code: 500,
                msg: '删除购物车失败'
            })
        } else {
            res.json({
                code: 200,
                msg: '删除购物车成功',
                data: results
            })
        }
    })
})
//查看购物车
router.get('/viewShopCart', function (req, res, next) {
    var products = req.query
    productDAL.viewShopCart(products, function (err, results) {
        if (err) {
            res.json({
                code: 500,
                msg: '查看购物车失败'
            })
        } else {
            res.json({
                code: 200,
                msg: '查看购物车成功',
                data: results
            })
        }
    })
})

//根据商品id获取商品更多图片信息
router.get('/getMoreProductPic', function (req, res, next) {

})
module.exports = router;
